#ifndef STATISTICS_
#define STATISTICS_
#include "ParseTree.h"
#include <iostream>
#include <string>
#include <tr1/unordered_map>
#include <stdio.h>
#include <vector>
#include <set>

using namespace std;
using namespace tr1;

class Relation{
    public:
        string relName;
        bool IsJoinedRelation;
        double numTuples;

        unordered_map<string, int> attributes;
        set<string> joinedRelations;

        Relation();
        ~Relation();
};

struct RelData
{
        //Relation *rel;
        string name;
        //int count;
};

class Statistics
{
    private:
        Relation *leftRelation;
        Relation *rightRelation;

    public:

        unordered_map<string, Relation*> relations;

        Statistics();
        Statistics(Statistics &copyMe);
        ~Statistics();

        void AddRel(char *relName, int numTuples);
        void AddAtt(char *relName, char *attName, int numDistincts);
        void CopyRel(char *oldName, char *newName);

        void Read(char *fromWhere);
        void Write(char *fromWhere);

        void  Apply(struct AndList *parseTree, char *relNames[], int numToJoin);
        double Estimate(struct AndList *parseTree, char **relNames, int numToJoin);

        int GetAttributeInfo(string attribute, char *relNames[], int numToJoin, bool setLeft);
        RelData GetAttributeInfo(string attribute, char *relNames[], int numToJoin);
        void ApplyCrossProduct(char *relNames[], int numToJoin);
        void ApplyJoinOrSelection(struct AndList *parseTree, char *relNames[], int numToJoin);
        bool IsValid(struct AndList *parseTree, char ** relNames, int numToJoin);
};

#endif
